package JzOffer.jz42;

import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author 张取
 * @version 1.0
 * @date 2022/1/9 上午11:34
 * JzOffer.jz42
 */

public class Solution02 {
    public static void main(String[] args){
        RecentCounter02 recentCounter = new RecentCounter02();
        System.out.println(recentCounter.ping(1));
        System.out.println(recentCounter.ping(100));
        System.out.println(recentCounter.ping(3001));
        System.out.println(recentCounter.ping(3002));
    }
}

class RecentCounter02 {

    private Queue<Integer> times;
    /**
     * windowSize: 窗口大小
     */
    private final int windowSize;

    public RecentCounter02() {
        times = new LinkedList<>();
        windowSize = 3000;
    }

    public int ping(int t) {
        times.offer(t);
        int frontElem;
        if(times.peek() != null){
            frontElem = times.peek();
        } else {
            return 0;
        }
        //滑动窗口，不满足窗口范围的元素滑出窗口
        while (frontElem + windowSize < t) {
            times.poll();
            if(times.peek() != null){
                frontElem = times.peek();
            } else {
                return 0;
            }
        }
        return times.size();
    }
}
